import {
	apiGetSmartResource,
	apiPostSmartResource
}  from './apiSmartResource';
import {developUrl,serverDK} from '@/views/api/api'



export const computedPostAlllSegment = (ev) => {/*获取总体概述*/
			 apiPostSmartResource(
			 	'dictionary/search',{"type":"segment"}
			 ).then((res)=>{
			 	if(res.data.code==='S100101'){
			 		ev.segmentOptions = res.data.data.list;
			 	}
			 })

};


export const computedPostSearch = (ev) => {/*获取搜索列表*/
			 apiPostSmartResource(
			 	'docker/strategy/list',ev.form
			 ).then((res)=>{
			 	if(res.data.code==='S100101'){
			 		ev.tableData = [];
			 		ev.loading = true;
			 		if(res.data.data.length!=0){
			 			ev.total=res.data.data[0].total;
			 			for (let i = 0; i < res.data.data.length; i++) {
				 			ev.tableData.push(
				 				{
				 					id :res.data.data[i].smAreaVo.id,
				 					segmentName:res.data.data[i].smAreaVo.segmentName,
				          			name:res.data.data[i].smAreaVo.name,
				          			name3:res.data.data[i].areaDockerStrategy===null?'否':'是',
				          			dockerCount:res.data.data[i].dockerCount===null?'0':res.data.data[i].dockerCount,
				          			dockerRunCount:res.data.data[i].dockerRunCount===null?'0':res.data.data[i].dockerRunCount,
				          			dockerErrorCount:res.data.data[i].dockerErrorCount===null?'0':res.data.data[i].dockerErrorCount,
				          			createBy:res.data.data[i].areaDockerStrategy===null?'无':(res.data.data[i].areaDockerStrategy.createBy===null?'无':res.data.data[i].areaDockerStrategy.createBy),
				          			createDate:res.data.data[i].areaDockerStrategy===null?'无':(res.data.data[i].areaDockerStrategy.createDate===null?'无':res.data.data[i].areaDockerStrategy.createDate),
				 					areaDockerStrategy:res.data.data[i].areaDockerStrategy,
				 				}
				 			)
			 			}
			 		}
			 	}
			 	ev.loading = false;
			 })

};


export const computedGetDockerDetail = (ev,id) => {/*获取详情*/
			 apiGetSmartResource(
			 	'docker/strategy/'+id+'/dockerStragety/detail'
			 ).then((res)=>{
			 	if(res.data.code==='S100101'){
			 		ev.dockerDetail = res.data.data.smAreaVo;
			 	}
			 })

};

export const computedAddDockerStragety = (ev,param) => {/*提交配置和更新配置*/
			
			if(param.id==0){
				apiPostSmartResource(
				 	'docker/strategy/add/dockerStragety',{
				 		"areaId":param.areaId,   //区域ID
						"onCpuUsed":param.onCpuUsed,  //CPU使用率
						"onMemoryUse":param.onMemoryUse,  //内存使用率
						"min":param.min,  //生成副本最少
						"max":param.max,  //生成副本最多
						"eventType":param.eventType.length==2?'3':param.eventType.join(','),
						"notifyTypes":param.notifyTypes.join(','),
				 	}
				 ).then((res)=>{
				 	if(res.data.code==='S100101'){
				 		ev.dialogVisible1 = false;
				 		ev.$message.success(res.data.msg);
				 		ev.search();
				 	}else{
				 		ev.$message.error(res.data.msg);
				 	}
				 	
				 	ev.closeLoading();
				 })
			}else{
				apiPostSmartResource(
				 	'docker/strategy/update/dockerStragety',{
				 		"id":param.id,
				 		"areaId":param.areaId,   //区域ID
						"onCpuUsed":param.onCpuUsed,  //CPU使用率
						"onMemoryUse":param.onMemoryUse,  //内存使用率
						"min":param.min,  //生成副本最少
						"max":param.max,  //生成副本最多
						"eventType":param.eventType.length==2?'3':param.eventType.join(','),
						"notifyTypes":param.notifyTypes.join(','),
				 	}
				 ).then((res)=>{
				 	if(res.data.code==='S100101'){
				 		ev.dialogVisible1 = false;
				 		ev.$message.success(res.data.msg);
				 		ev.search();
				 	}else{
				 		ev.$message.error(res.data.msg);
				 	}
				 	
				 	ev.closeLoading();
				 })
			}
};


export const computedAlertSearch=(ev)=>{/*告警日志列表*/
		apiPostSmartResource(
				 	'warnLog/list',{
				 		"warnArea":ev.form.warnArea,
						"startTime": ev.form.time[0],		
						"endTime": ev.form.time[1]		
				 	}
		).then((res)=>{
			if(res.data.code =='S100101'){
				for (let i = 0; i < res.data.data.length; i++) {
					res.data.data[i].warnItem = res.data.data[i].warnItem==1?'Docker运行异常告警':'生成Docker失败告警';
					res.data.data[i].isCreateCopy = res.data.data[i].isCreateCopy==1?'是':'否';
					res.data.data[i].createStatus = res.data.data[i].createStatus==1?'成功':'失败';
					res.data.data[i].isInform = res.data.data[i].isInform==1?'是':'否';
					res.data.data[i].status = res.data.data[i].status==1?'已处理':'未处理';
					res.data.data[i].isInform = res.data.data[i].isInform==1?'是':'否';
					res.data.data[i].dealNames =res.data.data[i].dealNames.length>0? res.data.data[i].dealNames[0]:'无';
				}
				ev.tableData = res.data.data;
			}
		})
}

export const warnLogArea=(ev)=>{/*获取告警区域*/
	apiPostSmartResource(
		'warnLog/warnArea',{}
	).then((res)=>{
		if(res.data.code =='S100101'){
			ev.areas = res.data.data
		}
		
	})
}


export const warnLogDetail=(ev,id,index)=>{/*获取告警详情*/
	
	ev.alertRiDetailForm.name1=ev.tableData[index].id;
	ev.alertRiDetailForm.name2=ev.tableData[index].warnItem;
	ev.alertRiDetailForm.name12=ev.tableData[index].warnTime;
	ev.alertRiDetailForm.name23=ev.tableData[index].warnArea;
	ev.alertRiDetailForm.name14=ev.tableData[index].isCreateCopy;
	ev.alertRiDetailForm.name25=ev.tableData[index].createStatus;
	ev.alertRiDetailForm.name1w=ev.tableData[index].isInform;
	ev.alertRiDetailForm.name2d=ev.tableData[index].informTime;
	ev.alertRiDetailForm.name1f=ev.tableData[index].status;
	ev.alertRiDetailForm.name2g=ev.tableData[index].dealNames;
	
	
	apiGetSmartResource(
		'warnLog/'+id+'/detail'
	).then((res)=>{
		if(res.data.code =='S100101'){
			if(res.data.data.warnLogDealList.length!=0){
				ev.alertRiDetail[1].elRow=[];
				let ww='';
				let yy='';
				for (let i = 0; i < res.data.data.warnLogDealList.length; i++) {
			//			ev.warnLogDealList[i]
						ev.alertRiDetail[1].elRow.push(
							[
			          						{
				          						name:'当前状态',
				          						value:'listState'+i
			          						},
			          						{
				          						name:'处理人员',
				          						value:'dealPerson'+i
			          						}
			          		],
						)
					ww='listState'+i;	
					yy='dealPerson'+i;	
					ev.alertRiDetailForm[ww]= res.data.data.warnLogDealList[i].dealResult==0?'成功':(res.data.data.warnLogDealList[i].dealResult==1?'处理中':'失败');
					ev.alertRiDetailForm[yy]= res.data.data.warnLogDealList[i].dealName;	
				}
			}
		}
		
	})
}



export const warnLogDown = (ev)=>{/*导出*/

	window.open(developUrl+serverDK+'warnLog/down?warnArea='+ev.form.warnArea+'&startTime='+ev.form.time[0]+'&endTime='+ev.form.time[1]); //在另外新建窗口中打开窗口
}

export const computedServiceSearch=(ev)=>{/*获取所有资源*/
	   ev.loading = ev.$loading({
					          lock: true,
					          text: 'Loading',
					          spinner: 'el-icon-loading',
					          background: 'rgba(0, 0, 0, 0.7)'
					        });
	apiPostSmartResource(
		'resource/service/search',{
			"infoTypeIds":ev.form.infoType,
			"businessTypeIds":ev.form.businessType,
			"name":ev.form.searchName,
			"isPageAble": false,//传这个不要分页了
			"serviceType":1
		}
	).then((res)=>{
		if(res.data.code =='S100101'){
			ev.resources = res.data.data.list;
			ev.loading.close();
			
		}
	})
}

export const computedGroupSearch=(ev)=>{/*获取所有资源*/
	if(ev.lastForm.hasOwnProperty('segmentId')){
		apiPostSmartResource(
		'resource/service/group/search',{
			"segmentId":ev.lastForm.segmentId,
			"businessTypes":ev.lastForm.businessType,
			"infoTypes":ev.lastForm.infoType,
			"parentResouceServiceId":ev.lastForm.parentResouceServiceId,
			"hasServicOutDataMetaType":1
		}
		).then((res)=>{
			if(res.data.code =='S100101'){
				if(res.data.data.length==0){
					ev.$message.error('无数据，请在基本信息中重新选择');
				}else{
					ev.dragItem = res.data.data;
				}
			}
		})
	}
	
}


export const computedResourceDetail=(ev)=>{/*获取所有资源*/
	 		apiGetSmartResource(
			 	'resource/service/'+ev.lastForm.parentResouceServiceId+'/detail'
			).then((res)=>{
			 	if(res.data.code==='S100101'){
			 		ev.serviceItem =[];
			 		ev.serviceItem.push(res.data.data)
			 		if(ev.choreDetailForm.smGroupResourceServices!=''){
			 			ev.dealUpdata(ev.choreDetailForm);
			 			computedDataSearch(ev);
//			 			ev.dealDataSerch();
			 		}
			 	}
			})
	
}



export const computedGroupSearch2=(ev)=>{/*获取所有资源2*/
	let inMetaDataIds = [];
	for (let i = 0; i < ev.lastForm.name9.length; i++) {
		inMetaDataIds.push(ev.lastForm.name9[i].id)
	}
	apiPostSmartResource(
		'resource/service/group/search',{
			"segmentId":ev.lastForm.segmentId,
			"inMetaDataIds":inMetaDataIds,  //请求参数中元数据的id
             "hasServicOutDataMetaType":1  //必须是1
		}
	).then((res)=>{
		if(res.data.code =='S100101'){
			if(res.data.data.length==0){
				ev.$message.error('无数据，请在基本信息中重新选择');
			}else{
				ev.dragItem = res.data.data;
				if(ev.choreDetailForm2.smGroupResourceServices!=''){
					ev.dealData();
				}
				
			}
		}
	})
}



export const computedDataSearch=(ev)=>{/*获取输入输出参数*/
	let resourceIds = [];
	for (let i = 0; i < ev.serviceItem.length; i++) {
		resourceIds.push(ev.serviceItem[i].id)
	}
	console.log(ev.serviceItem);
	console.log(resourceIds);
	apiPostSmartResource(
		'resource/service/data/search',
		{
			"resourceIds":resourceIds
		}
	).then((res)=>{
		if(res.data.code==='S100101'){
			if(ev.type=='yilai'){
				ev.choreInputp = res.data.data.inputDataFormList;
			}else if(ev.type=='zuhe'){
				for (let i = 0; i < res.data.data.inputDataFormList.length; i++) {/*过滤重复的元数据*/
					if(ev.hasYuanData.indexOf(res.data.data.inputDataFormList[i].metaDataId)==-1){
						ev.hasYuanData.push(res.data.data.inputDataFormList[i].metaDataId)
						ev.choreInputp.push(res.data.data.inputDataFormList[i])
					}
				}
			}
			
			for (let i = 0; i < res.data.data.outputDataFormList.length; i++) {
				if(ev.hasOutPut.indexOf(res.data.data.outputDataFormList[i].metaDataId)==-1){
					ev.hasOutPut.push(res.data.data.outputDataFormList[i].metaDataId)
					ev.outputs.push(res.data.data.outputDataFormList[i])
				}
			}
//			ev.outputs = res.data.data.outputDataFormList;
			if(ev.type=='yilai'){
				ev.dealDataSerch();
			}
			if(ev.type=='zuhe'){
				if(ev.choreDetailForm2.smGroupOutputFieldViewVos!=''){
					ev.dealOutPut();
				}
			}
			
		}
		
	})
}


export const computedOrganizationSearch=(ev)=>{
	
	apiPostSmartResource(
		'organization/search',
		{
			   "type":"1",  //0是请求，1是服务
    		   "isPageAble": "false"  //默认不分页
		}
	).then((res)=>{
		ev.everyAreaCheckBox = res.data.data.list
		if(ev.chenueForm.requesterIdList!=''){
        		ev.dealData();
        }
	})
}

export const computedGroupPublic=(ev)=>{/*服务编排注册*/
	console.log(ev.form);
	if(ev.form.hasOwnProperty('nopublicId')){
		apiPostSmartResource(
			'resource/service/shareUpdate',ev.form
		).then((res)=>{
			if(res.data.code==='S100101'){
				ev.$message.success('操作成功，俩秒后刷新');
				setTimeout(function(){
					window.location.reload();
				},2000)
			}else{
				ev.$message.error(res.data.msg);
			}
		})
	}else{
		apiPostSmartResource(
		'resource/service/group/public',ev.form
		).then((res)=>{
			if(res.data.code==='S100101'){
				ev.$message.success('操作成功，俩秒后刷新');
				setTimeout(function(){
					window.location.reload();
				},2000)
			}else{
				ev.$message.error(res.data.msg);
			}
		})
	}
	
}


export const computedMetaDataSearchByType=(ev,metaDataType)=>{/*获取元数据*/
	apiGetSmartResource(
		'metaData/'+metaDataType+'/searchByType',
	).then((res)=>{
		if(res.data.code==='S100101'){
			ev.metaData = res.data.data;
		}
		
	})
}



export const computedSegmentSearch=(ev)=>{
	apiGetSmartResource('segment/selectAll').then((res)=>{
		if(res.data.code==='S100101'){
			ev.segment = res.data.data;
		}
	})
}


export const componentsGetDetail=(ev)=>{/*服务编排的详情*/
	apiGetSmartResource(
		'resource/service/groupDetail/'+ev.form.nopublicId
		).then((res)=>{
			if(res.data.code==='S100101'){
				/*base-info部分*/
				ev.baseInfoForm.name = res.data.data.smResourceServiceVo.name
				ev.baseInfoForm.describe = res.data.data.smResourceServiceVo.describe
				ev.baseInfoForm.segmentId = res.data.data.smResourceServiceVo.segmentId
				ev.baseInfoForm.type = res.data.data.smResourceServiceVo.type
				ev.baseInfoForm.nodeIds = res.data.data.smResourceServiceVo.nodeIds
				ev.baseInfoForm.businessType = res.data.data.smResourceServiceVo.businessType
				ev.baseInfoForm.infoType = res.data.data.smResourceServiceVo.infoType
				
				/*base-info部分*/
				/*服务编排部分*/
				if(res.data.data.smResourceServiceVo.type==3){/*依赖关系*/
					ev.baseInfoForm.parentResouceServiceId = res.data.data.smGroupResourceServices[0].parentResourceServiceId
					/*依赖关系的表单*/
					ev.choreDetailForm.smGroupResourceServices = res.data.data.smGroupResourceServices;
					ev.choreDetailForm.smGroupOutputFieldViewVos = res.data.data.smGroupOutputFieldViewVos
				}else if (res.data.data.smResourceServiceVo.type==4){/*组合关系*/
					ev.choreDetailForm2.smGroupResourceServices = res.data.data.smGroupResourceServices
					let hasSmMetaData = [];
					for (let i = 0; i < res.data.data.smMetaData.length; i++) {/*这是效率最高的筛选*/
						if(hasSmMetaData.indexOf(res.data.data.smMetaData[i].id)==-1){
							hasSmMetaData.push(res.data.data.smMetaData[i].id)
							ev.baseInfoForm.smMetaData.push(res.data.data.smMetaData[i])
						}
					}
					ev.choreDetailForm2.smGroupOutputFieldViewVos = res.data.data.smGroupOutputFieldViewVos;

				}
				/*服务策略部分*/
				if(res.data.data.smResourceServiceVo.accessTactics!=null){
					ev.chenueForm.accessTactics = res.data.data.smResourceServiceVo.accessTactics
				}
				ev.chenueForm.smGroupOutputFieldViewVos = res.data.data.smGroupOutputFieldViewVos
				ev.chenueForm.businessType1 = res.data.data.smArea;
				ev.chenueForm.requesterIdList = res.data.data.smAuthResourceServiceVos;
			}
	})
}
